package com.demo2do.fms.admin.service.report.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.page.Page;
import com.demo2do.fms.admin.entity.report.export.DriverDistanceReportSearch;
import com.demo2do.fms.admin.entity.report.export.ReimbursementReportSearch;
import com.demo2do.fms.admin.entity.report.export.TruckDistanceReportSearch;
import com.demo2do.fms.admin.handler.support.FmsJdbcSupport;
import com.demo2do.fms.admin.service.report.ExportService;

/**
 * 
 * @author sanz
 *
 */
@Service("exportService")
public class ExportServiceImpl implements ExportService {
	
	@Autowired
	private FmsJdbcSupport fmsJdbcSupport;

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.fms.admin.service.export.ExportService#count(com.demo2do.fms.admin.entity.report.export.ReimbursementReportSearch)
	 */
    public int count(ReimbursementReportSearch search) {
        String sentence = search.buildCountTemplate();
        Map<String, Object> parameters = search.buildParameters();
        return fmsJdbcSupport.queryForInt(sentence, parameters);
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.export.ExportService#search(com.demo2do.fms.admin.entity.report.export.ReimbursementReportSearch, com.demo2do.core.entity.page.Page)
     */
    public List<Map<String, Object>> search(ReimbursementReportSearch search, Page page) {
        
        // 1. calculate page
        Integer beginIndex = page.getBeginIndex() + 1;
        search.setBeginIndex(beginIndex);
        
        Integer endIndex = page.getCurrentPage() * page.getEveryPage();
        search.setEndIndex(endIndex);
        
        // 2. build parameters
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        
        // 3. do query
        List<Map<String, Object>> result = fmsJdbcSupport.queryForList(sentence, parameters, search.buildRowMapper());
        page.calculate();
        return result;
        
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.export.ExportService#count(com.demo2do.fms.admin.entity.report.export.DriverDistanceReportSearch)
     */
    public int count(DriverDistanceReportSearch search) {
        String sentence = search.buildCountTemplate();
        Map<String, Object> parameters = search.buildParameters();
        return fmsJdbcSupport.queryForInt(sentence, parameters);
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.export.ExportService#search(com.demo2do.fms.admin.entity.report.export.DriverDistanceReportSearch, com.demo2do.core.entity.page.Page)
     */
    public List<Map<String, Object>> search(DriverDistanceReportSearch search, Page page) {
        
        // 1. calculate page
        Integer beginIndex = page.getBeginIndex() + 1;
        search.setBeginIndex(beginIndex);
        
        Integer endIndex = page.getCurrentPage() * page.getEveryPage();
        search.setEndIndex(endIndex);
        
        // 2. build parameters
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        
        // 3. do query
        List<Map<String, Object>> result = fmsJdbcSupport.queryForList(sentence, parameters, search.buildRowMapper());
        page.calculate();
        return result;
        
    }
	
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.export.ExportService#count(com.demo2do.fms.admin.entity.report.export.DriverDistanceReportSearch)
     */
    public int count(TruckDistanceReportSearch search) {
        String sentence = search.buildCountTemplate();
        Map<String, Object> parameters = search.buildParameters();
        return fmsJdbcSupport.queryForInt(sentence, parameters);
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.export.ExportService#search(com.demo2do.fms.admin.entity.report.export.DriverDistanceReportSearch, com.demo2do.core.entity.page.Page)
     */
    public List<Map<String, Object>> search(TruckDistanceReportSearch search, Page page) {
        
        // 1. calculate page
        Integer beginIndex = page.getBeginIndex() + 1;
        search.setBeginIndex(beginIndex);
        
        Integer endIndex = page.getCurrentPage() * page.getEveryPage();
        search.setEndIndex(endIndex);
        
        // 2. build parameters
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        
        // 3. do query
        List<Map<String, Object>> result = fmsJdbcSupport.queryForList(sentence, parameters, search.buildRowMapper());
        page.calculate();
        return result;
        
    }
}
